package ThreadTest_p.useSleep.v1;

import java.util.concurrent.atomic.AtomicLong;

public class UseSleep {
    public static void main(String[] args) throws InterruptedException {
        // AtomicLong s1= new AtomicLong();
        // Long[] s = new Long[1];
        AtomicLong s1= new AtomicLong();
        Thread t = new Thread(() -> {
            // long s1 = 0;
            try {
                //s1.set(System.currentTimeMillis());
                s1.set(System.currentTimeMillis());
                System.out.println("开始休眠前 中断标志: "+Thread.currentThread().isInterrupted()); // 开始休眠前 中断标志: false
                System.out.println("开始休眠");
                Thread.sleep(5000); // 休眠5秒
                long s2 = System.currentTimeMillis(); // 没有被执行
                System.out.println("休眠结束,耗时: "+(s2- s1.get())+" 中断标志: "+Thread.currentThread().isInterrupted()); // 没有被执行
            } catch (InterruptedException e) {
                long s3 = System.currentTimeMillis();
                System.out.println("被中断了,开始休眠到中断休眠耗时:"+(s3- s1.get())+" 中断后标志"+Thread.currentThread().isInterrupted()); // 被中断了,开始休眠到中断休眠耗时:1006 中断后标志false
                Thread.currentThread().interrupt();
                System.out.println("重新设置中断状态: " + Thread.currentThread().isInterrupted()); // 重新设置中断状态: true
            }
            System.out.println("休眠结束");
        });

        t.start();
        Thread.sleep(1000); // 主线程休眠1秒
        t.interrupt(); // 中断线程
    }
}
